/*
 * @Date: 2023-02-27 20:40:00
 * @LastEditors: 于波
 * @LastEditTime: 2023-02-27 20:57:16
 * @Description: 路由
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import HomeView from '../views/Home.vue';
import Admin from '@/admin/Admin.vue';
import Search from '@/views/Search.vue';
import PersonCenter from '@/views/PersonCenter.vue';
import Write from '@/views/Write.vue';
import Persion from '@/views/Persion.vue';
import Article from '@/views/Article.vue';
import Home from '@/admin/Home.vue';
import Banner from '@/admin/Banner.vue';
import Type from '@/admin/Type.vue';
import Verify from '@/admin/Verify.vue';
import UserVue from '@/admin/User.vue';
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
    },
    {
        path: '/article/:id',
        name: 'article',
        component: Article,
    },
    {
        path: '/search',
        name: 'search',
        component: Search,
    },
    {
        path: '/personCenter',
        name: 'personCenter',
        component: PersonCenter,
    },
    {
        path: '/write/:id*',
        name: 'write',
        component: Write,
    },

    {
        path: '/person',
        name: 'person',
        component: Persion,
    },
    {
        path: '/adminLogin',
        name: 'adminLogin',
        component: Admin,
    },
    {
        path: '/admin',
        name: 'admin',
        component: Home,
        children: [
            {
                path: '',
                name: 'Banner',
                component: Banner,
                meta:{
                    auth:true
                }
            },
            {
                path: 'type',
                name: 'type',
                component: Type,
                meta:{
                    auth:true
                }
            },
            {
                path: 'verify',
                name: 'verify',
                component: Verify,
                meta:{
                    auth:true
                }
            },
            {
                path: 'user',
                name: 'user',
                component: UserVue,
                meta:{
                    auth:true
                }
            },
        ],
    },
];

const router = createRouter({
    history: createWebHistory('/'),
    routes,
});

router.beforeEach((to, from, next) => {
    if (to.meta.auth) {
        const token = localStorage.getItem('adminToken');
        if (token) {
            next();
        } else {
            next({
                path: '/adminLogin',
            });
        }
    } else {
        next();
    }
});

export default router;
